#!coding=utf-8
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.shortcuts import *
from models import *
from django.views.decorators.csrf import csrf_exempt
from util.form_util import *
import json
from django.core import serializers

#根据化学品名称查询企业化学产品数量
@csrf_exempt
def productListAjax(request):
    name = ""
    if "name" in request.GET:
        name = request.GET['name']
    if "name" in request.POST:
        name = request.POST['name']
    query = '''select
                    e.name,
                    e.id,
                    count(p.id) as count,
                    i.geo_points,
                    i.geo_center_point,
                    i.geo_extent,
                    p.id,
                    p.name
                from
                    enterprise_chemicals_product as p,
                    enterprise_basic_information_enterprise_basic_information as i,
                    enterprise_basic_information_enterprise as e
                where 
                    i.enterprise_id = e.id and
                    p.enterprise_id = e.id and
                    p.name = %s
                group by p.enterprise_id
            '''
    from django.db import connection,transaction
    cursor = connection.cursor()
    cursor.execute(query,[name])
    raw = cursor.fetchall()
    list = []
    for i in raw:
        bak = {"name":i[0],"id":i[1],"count":i[2],"geo_points":i[3],"geo_center_point":i[4],"geo_extent":i[5]}
        list.append(bak)
    return HttpResponse(json.dumps(list,ensure_ascii=False))

#原辅材料输入及使用企业列表
@csrf_exempt
def materialListAjax(request):
    name = ""
    if "name" in request.GET:
        name = request.GET['name']
    if "name" in request.POST:
        name = request.POST['name']
    query = """select
                    e.name,
                    e.id,
                    COUNT(m.id) AS count,
                    i.geo_points,
                    i.geo_center_point,
                    i.geo_extent
                FROM
                    enterprise_basic_information_enterprise e

                JOIN enterprise_chemicals_materials m ON m.enterprise_id = e.id

                LEFT JOIN enterprise_basic_information_enterprise_basic_information i ON i.enterprise_id = e.id

                WHERE m.chemicals_name = %s 

                GROUP BY
                    e.id
            """
    from django.db import connection,transaction
    cursor = connection.cursor()
    cursor.execute(query,[name])
    raw = cursor.fetchall()
    list = []
    for i in raw:
        bak = {"name":i[0],"id":i[1],"count":i[2],"geo_points":i[3],"geo_center_point":i[4],"geo_extent":i[5]}
        list.append(bak)
    return HttpResponse(json.dumps(list,ensure_ascii=False))


#主要产品(含副产品和中间体) 及输出列表企业列表
@csrf_exempt
def chemicalsListAjax(request):
    name = ""
    if "name" in request.GET:
        name = request.GET['name']
    if "name" in request.POST:
        name = request.POST['name']
    query = """select c.`name`,c.id,COUNT(*) as count,s.geo_points,s.geo_center_point  from enterprise_basic_information_enterprise c 
                JOIN enterprise_chemicals_chemicals m on m.enterprise_id = c.id 
                left join enterprise_basic_information_enterprise_basic_information s on s.enterprise_id = c.id
                WHERE m.name = %s GROUP BY c.id"""
    from django.db import connection,transaction
    cursor = connection.cursor()
    cursor.execute(query,[name])
    raw = cursor.fetchall()
    list = []
    print list.__len__()
    print "---------------------------"
    for i in raw:
        bak = {"name":i[0],"id":i[1],"count":i[2],"geo_points":i[3],"geo_center_point":i[4]}
        list.append(bak)
    return HttpResponse(json.dumps(list,ensure_ascii=False))